#pragma once
#include<iostream>
#include<cstring>
#include<memory>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include"log.hpp"

class InetAddr{
    private:
        void PortNet2Host()
        {
            _port = ::ntohs(_addr.sin_port);
        }
        void IpNet2Host()
        {
            char inbuffer[64];
            const char *outbuffer = ::inet_ntop(AF_INET, &_addr.sin_addr, inbuffer, sizeof(inbuffer));
            _ip = inbuffer;
        }
    public:
        InetAddr()
        {

        }
        InetAddr(const struct sockaddr_in &addr):_addr(addr)//网络转主机
        {
            PortNet2Host();
            IpNet2Host();
        }
        InetAddr(uint16_t port)//主机转网络
            :_port(port),_ip("")
        {
            _addr.sin_family = AF_INET;
            _addr.sin_port = ::htons(_port);
            _addr.sin_addr.s_addr = INADDR_ANY;
        }
        struct sockaddr_in* GetAddr() { return &_addr; }

        socklen_t GetAddrLen() const { return sizeof(struct sockaddr_in); }
        
        std::string GetIp() const { return _ip; }
        
        uint16_t GetPort() const { return _port; }
        
        std::string GetIpPort() const
        {
            return _ip + ":" + std::to_string(_port);
        }
        bool operator==(const InetAddr& other)
        {
            return _ip==other._ip && _port==other._port;
        }
        ~InetAddr()
        {
            
        }
    private:
        struct sockaddr_in _addr;
        std::string _ip;
        uint16_t _port;
};